/**
 *  File: CollectionEditor.java
 *  Copyright (c) 2010
 *  phyokyaw
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package uk.ac.durham.tel.synergynet.ats.collections.ui.editors;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.part.EditorPart;

import uk.ac.durham.tel.synergynet.ats.collections.model.CollectionMedia;
import uk.ac.durham.tel.synergynet.ats.collections.model.CollectionNode;
import uk.ac.durham.tel.synergynet.ats.collections.ui.AbstractMediaCollectionControl;
import uk.ac.durham.tel.synergynet.ats.collections.ui.MediaPreviewControl;
import uk.ac.durham.tel.synergynet.ats.media.model.MediaNode;
import uk.ac.durham.tel.synergynet.ats.navigation.NavigatorLabelProvider;
import uk.ac.durham.tel.synergynet.ats.navigation.model.INode;
import uk.ac.durham.tel.synergynet.ats.projects.model.ProjectNode;
import uk.ac.durham.tel.synergynet.ats.projects.ui.NodeEditorInput;
import uk.ac.durham.tel.synergynet.ats.resource.ResourceLoader;
import uk.ac.durham.tel.synergynet.ats.timebar.MediaTimeBar;


/**
 * @author phyokyaw
 *
 */
public class CollectionEditor extends EditorPart {

	public static final String ID = "uk.ac.durham.tel.synergynet.ats.collections.ui.editors.collectionMediaEditor";

	private CollectionNode _collectionNode;
	private MediaPreviewControl _mediaPreviewControl;
	private AbstractMediaCollectionControl _mediaCollectionControl;

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
		//
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
	 */
	@Override
	public void doSaveAs() {
		//
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
	 */
	@Override
	public void init(IEditorSite site, IEditorInput input)
	throws PartInitException {

		this.setSite(site);
		this.setInput(input);

		_collectionNode = (CollectionNode) ((NodeEditorInput) input).getNode();
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#isDirty()
	 */
	@Override
	public boolean isDirty() {
		return false;
	}


	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
	 */
	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		SashForm container = new SashForm(parent, SWT.VERTICAL);
		addMediaPreview(container);
		addMediaController(container);
		MenuManager menuManager = new MenuManager();
		menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		_mediaCollectionControl.getTimeBarViewer().setMenu(menuManager.createContextMenu(_mediaCollectionControl.getTimeBarViewer()));
		getSite().registerContextMenu(menuManager, _mediaCollectionControl.getTimeBarViewer());
		getSite().setSelectionProvider(_mediaCollectionControl.getTimeBarViewer());
		setPartName(String.format("%s (Collection)", _collectionNode.getLabel()));
		
	}
	
	public void addCollectionMedia() {
		List<String> existingMediaNames = new ArrayList<String>();
		for (CollectionMedia media : _mediaCollectionControl.getCollectionMediaList()) {
			existingMediaNames.add(media.getMediaName());
		}
		List<MediaNode> selectedMediaNodes = this.showMediaSelection(existingMediaNames.toArray(new String[]{}), _mediaCollectionControl);
		if (selectedMediaNodes != null)
		for(MediaNode node : selectedMediaNodes)
			_mediaCollectionControl.addMedia(node);
		
	}
	
	private List<MediaNode> showMediaSelection(String[] excludeItemList,
			Composite owner) {
		List<INode> availableMediaList = ((ProjectNode)_collectionNode.getLastParent()).getMediaRootNode().getMediaNodes(
						excludeItemList);
		if (availableMediaList.size() >= 0) {
			ElementListSelectionDialog dialog = new ElementListSelectionDialog(
					owner.getShell(), new NavigatorLabelProvider());
			dialog.setMultipleSelection(true);
			dialog.setElements(availableMediaList
					.toArray(new INode[availableMediaList.size()]));
			dialog.setTitle(ResourceLoader
					.getString("SESSION_PROPERTY_MEDIA_SELECTOR_TITLE"));
			dialog.open();
			Object[] selectedObjects = dialog.getResult();
			List<MediaNode> nodeList = new ArrayList<MediaNode>();
			if (selectedObjects != null) {
				
				for(Object node : selectedObjects) {
					nodeList.add((MediaNode) node);
				}
			}
			return nodeList; 
			
			
		} else {
			MessageDialog.openError(owner.getShell(), ResourceLoader
					.getString("DIALOG_ERROR_TITLE"), ResourceLoader
					.getString("SESSION_PROPERTY_MEDIA_SELECTOR_NO_MEDIA"));
			return null;
		}
	}
	

	private void addMediaPreview(Composite parent) {
		Composite container = new Composite(parent, SWT.BORDER | SWT.CENTER);
		container.setLayout(new GridLayout());
		_mediaPreviewControl = new MediaPreviewControl(container, SWT.NONE);
		_mediaPreviewControl.setLayoutData(new GridData(GridData.FILL_BOTH));
	}


	/**
	 * 
	 * To add media controller
	 * 
	 */
	private void addMediaController(Composite parent) {
		Composite container = new Composite(parent, SWT.BORDER | SWT.CENTER);
		container.setLayout(new GridLayout());
		_mediaCollectionControl = new MediaTimeBar(container,  SWT.NULL,_mediaPreviewControl.getObservableMediaPreviewList(), _collectionNode, ((ProjectNode)_collectionNode.getLastParent()).getMediaRootNode());
		_mediaCollectionControl.setLayoutData(new GridData(GridData.FILL_BOTH));
	}


	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		//
	}

	/**
	 * 
	 */
	public void addCollectionMediaClip() {
		_mediaCollectionControl.addMediaClip();
	}
	
	public boolean isMediaAdded() {
		return !_mediaCollectionControl.getCollectionMediaList().isEmpty();
	}

}

